<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Model {

	function getPageUsers($page){
    	$amount = 20;
    	$start = ($page-1)*20;
        $data = $this->db->get('users', $amount, $start);
        return $data->result();
    }

    function addUser($name, $email, $pass, $city){
        $email = strtolower($email);
    	$data = array(
    		 'name' => $name,
    		 'email' => $email,
    		 'pass' => $pass,
             'city' => $city
    		 );
    	$this->db->insert('users', $data);
    }

    function editUser($id, $name, $email, $position, $pass){
        $email = strtolower($email);
    	$this->db->where('id',$id);
    	$this->db->set('name', $name);
		$this->db->set('email', $email);
		$this->db->set('pass', $pass);
		$this->db->update('users');
    }

    function userExist($email, $cipheredpass){
        $email = strtolower($email);
    	$array = array('email' => $email, 'pass' => $cipheredpass);
    	$this->db->where($array);
    	$query = $this->db->get('users');
    	if($query->num_rows() > 0 ){
    		return $query->result();
    	}else{
    		return false;
    	}
    }

    function getUserById($id){
    	$this->db->where('id', $id);
    	$data = $this->db->get('users');
        return $data->result();
    }

	function pageCount(){
		$query = $this->db->get('users');
		$rowcount = $query->num_rows();
		$pagecount = 0;
		if($rowcount % 20 == 0){
			$pagecount = intval($rowcount/20);
		}else{
			$pagecount = intval($rowcount/20)+1;
		}
		return $pagecount;
	}

    function getUserAvatar($id){
        $this->db->where('id', $id);
        $data = $this->db->get('users');
        return $data->result();
    }
}